import numpy as np
import matplotlib.pyplot as plt
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import confusion_matrix, classification_report, accuracy_score, precision_score, recall_score, f1_score
import os
import pickle

# load data
path = r'../../../../../large_data/hand_writing/'
data = np.loadtxt(path + 'imgX.txt', delimiter=',')
y = np.loadtxt(path + 'labely.txt', delimiter=',')
m = len(y)
y[y == 10] = 0  # ATTENTION

# scale data
mu = data.mean()
sigma = data.std()
data -= mu
data /= sigma

# shuffle data
rnd_idx = np.random.permutation(m)
data = data[rnd_idx]
y = y[rnd_idx]
x = data

# splice data
XX = np.c_[np.ones(m), data]

# onehot
y_onehot = np.zeros([m, 10])
for k, v in enumerate(y):
    y_onehot[k, int(v)] = 1
# print(y[:10])
# print(y_onehot[:10])

# split data
m_train = int(0.7 * m)
m_test = m - m_train
x_train, x_test = np.split(x, [m_train])
y_train, y_test = np.split(y, [m_train])
XX_train, XX_test = np.split(XX, [m_train])
y_onehot_train, y_onehot_test = np.split(y_onehot, [m_train])

savepath = os.path.basename(__file__) + '_v3.0.tmp.dat'
if os.path.exists(savepath):
    xfile_r = open(savepath, 'rb')
    model = pickle.load(xfile_r)
    print('LOADED MODEL')
else:
    print('TRAINING...')
    # Supported activations are ('identity', 'logistic', 'tanh', 'relu').
    model = MLPClassifier([100, 64], 'relu', alpha=0.1, max_iter=200)
    model.fit(x_train, y_train)
    xfile_w = open(savepath, 'wb')
    pickle.dump(model, xfile_w)
# print('coefs_', model.coefs_)
# print('intercepts_', model.intercepts_)
print('Training score', model.score(x_train, y_train))
print('Testing score', model.score(x_test, y_test))

h_test = model.predict(x_test)
print('confusion matrix')
print(confusion_matrix(y_test, h_test))
print('acc', accuracy_score(y_test, h_test))
print('precision', precision_score(y_test, h_test, average='micro'))
print('recall', recall_score(y_test, h_test, average='micro'))
print('f1', f1_score(y_test, h_test, average='micro'))
